package com.nexgo.payment.trans.scanquery;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.Gravity;

import com.nexgo.data.json.JsonRecData;
import com.nexgo.data.json.JsonTools;
import com.nexgo.data.json.bean.JsonBody;
import com.nexgo.payment.R;
import com.nexgo.payment.action.inputadminpwd.AdminPasswordFragment;
import com.nexgo.payment.action.inputpaycode.InputPayCodeFragment;
import com.nexgo.payment.action.inputtracecode.InputTraceCodeFragment;
import com.nexgo.payment.action.scan.ScanFragment;
import com.nexgo.payment.constrant.SysConstant;
import com.nexgo.payment.databases.Repository;
import com.nexgo.payment.fragment.TextDialogFragment;
import com.nexgo.payment.scan.ScanCameraActivity;
import com.nexgo.payment.scan.ScanNFragment;
import com.nexgo.payment.trans.AbsTransActivity;
import com.nexgo.payment.util.ActivityUtils;
import com.nexgo.payment.util.MessageHelper;
import com.nexgo.payment.util.StringUtils;
import com.nexgo.payment.util.ToastUtils;

/**
 * 扫码退货
 *
 * @author 谭忠扬-YuriTam
 * @time 2017年9月12日
 */
public class ScanQueryActivity extends AbsTransActivity<ScanQueryContract.Presenter> implements ScanQueryContract.View{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        new ScanQueryPresenter(this, Repository.getInstance());
    }

    @Override
    protected void onResume() {
        super.onResume();
        mPresenter.start();
        //第一次调用不起作用，扫码返回后再次调用才起作用
        mPresenter.showOnScanNextAndRestart();
    }

    @Override
    public void showInputAdminPassword() {
        AdminPasswordFragment adminPwdFragment = AdminPasswordFragment.newInstance();
        Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.contentFrame);
        if (fragment == null) {
            ActivityUtils.addFragmentToActivity(getSupportFragmentManager(), adminPwdFragment, R.id.contentFrame);
        } else {
            ActivityUtils.replaceFragmentToActivity(getSupportFragmentManager(), adminPwdFragment, R.id.contentFrame);
        }
    }

    @Override
    public void showInputTraceCode() {
        InputPayCodeFragment traceCodeFragment = InputPayCodeFragment.newInstance(false);
        Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.contentFrame);
        if (fragment == null) {
            ActivityUtils.addFragmentToActivity(getSupportFragmentManager(), traceCodeFragment, R.id.contentFrame);
        } else {
            ActivityUtils.replaceFragmentToActivity(getSupportFragmentManager(), traceCodeFragment, R.id.contentFrame);
        }
    }

 /*   @Override
    public void showScanFragment() {
        ScanFragment scanFragment = ScanFragment.newInstance();
        Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.contentFrame);
        if (fragment == null) {
            ActivityUtils.addFragmentToActivity(getSupportFragmentManager(), scanFragment, R.id.contentFrame);
        } else {
            ActivityUtils.replaceFragmentToActivity(getSupportFragmentManager(), scanFragment, R.id.contentFrame);
        }
    }*/
 @Override
 public void showScanFragment() {
        /*ScanFragment scanFragment = ScanFragment.newInstance();
        Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.contentFrame);
        if (fragment == null) {
            ActivityUtils.addFragmentToActivity(getSupportFragmentManager(), scanFragment, R.id.contentFrame);
        } else {
            ActivityUtils.replaceFragmentToActivity(getSupportFragmentManager(), scanFragment, R.id.contentFrame);
        }*/
     startActivityForResult(ScanCameraActivity.newInstance(this),ScanCameraActivity.SCAN_REQUEST_CODE);
 }

    private void addScanNFragment(){
        ScanNFragment scanFragment = ScanNFragment.newInstance();
        Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.contentFrame);
        if (fragment == null) {
            ActivityUtils.addFragmentToActivity(getSupportFragmentManager(), scanFragment, R.id.contentFrame);
        } else {
            ActivityUtils.replaceFragmentToActivity(getSupportFragmentManager(), scanFragment, R.id.contentFrame);
        }
    }



    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == ScanCameraActivity.SCAN_REQUEST_CODE){
            if(resultCode == ScanCameraActivity.SCAN_RESULT_SUCCESS_CODE){
                if(data == null || TextUtils.isEmpty(data.getStringExtra(ScanCameraActivity.RESULT_MSG))){
                    ToastUtils.showToast("扫码失败");
                    finish();
                }
                addScanNFragment();
                mPresenter.inputScanNext(data.getStringExtra(ScanCameraActivity.RESULT_MSG));
                //第一次调用不起作用，扫码返回后再次调用才起作用
                mPresenter.showOnScanNextAndRestart();
                return;
            }
            if(data != null){
                String eMsg =  data.getStringExtra(ScanCameraActivity.RESULT_MSG);
                ToastUtils.showToast(eMsg);
                finish();
            }

        }
    }
    @Override
    public void onShowScanQueryMsg(JsonRecData jsonRecData) {
        JsonBody jsonBody = JsonTools.fromJson(jsonRecData.getREQ_BODY().getDECRYPT(),JsonBody.class);
        TextDialogFragment dialogFragment = TextDialogFragment.newInstance();
        dialogFragment.setDialogType(TextDialogFragment.DIALOG_TYPE_SUCCESS);
        dialogFragment.setTitleText(getString(R.string.scan_query_succ));
        StringBuilder showString = new StringBuilder();
        if(jsonBody.getSys_order_no() != null && !jsonBody.getSys_order_no().equals("")) {
            showString.append("订单号:");
            showString.append(jsonBody.getSys_order_no());

        }
        if(jsonBody.getTotal_amount() != null && !jsonBody.getTotal_amount().equals("")){
            showString.append("\n交易金额:");
            showString.append(StringUtils.formatAmount(jsonBody.getTotal_amount(), SysConstant.FUND_DIGITS));
        }
        if(jsonBody.getRefund_amount() != null && !jsonBody.getRefund_amount().equals("")) {
            showString.append("\n退货金额:");
            showString.append(StringUtils.formatAmount(jsonBody.getRefund_amount(), SysConstant.FUND_DIGITS));
        }
        if(jsonBody.getTxn_state() != null && !jsonBody.getTxn_state().equals("")){
            String str = MessageHelper.getScanStateCodeErrorInfo(jsonBody.getTxn_state());
            showString.append("\n交易状态:");
            showString.append(str);
        }
        dialogFragment.setContentText(showString.toString(), Gravity.LEFT);

        dialogFragment.setPositionText(getString(R.string.confirm));
        dialogFragment.setDownTimer(SysConstant.TIME_OUT_10, true);
        dialogFragment.setDialogListener(new TextDialogFragment.OnDialogListener() {
            @Override
            public void onCancel() {
                quitTrans();
            }

            @Override
            public void onConfirm() {
                quitTrans();
            }
        });
        dialogFragment.show(getSupportFragmentManager(), null);

    }

    public static Intent newInstance(Context context) {
        return new Intent(context, ScanQueryActivity.class);
    }

}
